﻿using CodeTemplate.DbHelper.CoreObj;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace CodeTemplate.CodeEdit
{
    public partial class FrmEditForTable : Form
    {
        private List<ObjJsField> jsFields;

        public List<ObjJsField> JsFields
        {
            get { return jsFields; }
        }

        public FrmEditForTable(List<ObjJsField> jsFields)
        {
            InitializeComponent();
            this.StartPosition = FormStartPosition.CenterParent;
            this.jsFields = jsFields;
            this.gridControl1.DataSource = this.jsFields;
        }

        private void gridControl1_EmbeddedNavigator_ButtonClick(object sender, DevExpress.XtraEditors.NavigatorButtonClickEventArgs e)
        {
            ObjJsField obj = this.gridView1.GetRow(this.gridView1.FocusedRowHandle) as ObjJsField;
            int selectIndex = this.jsFields.IndexOf(obj);
            switch (e.Button.Hint)
            {
                case "上移":
                    if (selectIndex > 0)
                    {
                        ObjJsField tmpObj = this.jsFields[selectIndex-1];
                        this.jsFields[selectIndex-1]= this.jsFields[selectIndex];
                        this.jsFields[selectIndex] = tmpObj;
                        this.gridView1.FocusedRowHandle--;
                    }
                    break;
                case "下移":
                    if (selectIndex < (this.jsFields.Count -1))
                    {
                        ObjJsField tmpObj = this.jsFields[selectIndex + 1];
                        this.jsFields[selectIndex + 1] = this.jsFields[selectIndex];
                        this.jsFields[selectIndex] = tmpObj;
                        this.gridView1.FocusedRowHandle++;
                    }
                    break;
            }
            this.gridControl1.DataSource = this.jsFields;
            this.gridControl1.RefreshDataSource();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }

        private void btnOk_Click(object sender, EventArgs e)
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
    }
}
